package com.situ.gym.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.gym.mapper.AdminRoleMapper;
import com.situ.gym.mapper.RoleMapper;
import com.situ.gym.pojo.Admin;
import com.situ.gym.mapper.AdminMapper;
import com.situ.gym.pojo.AdminRole;
import com.situ.gym.pojo.Role;
import com.situ.gym.pojo.query.AdminQuery;
import com.situ.gym.pojo.vo.PermissionVO;
import com.situ.gym.pojo.vo.RouterVO;
import com.situ.gym.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.gym.util.Result;
import com.situ.gym.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-08-22
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private PermissionServiceImpl permissionServiceImpl;

    @Override
    public IPage<Admin> list(AdminQuery adminQuery) {
        //Map<String, Object>  map = ThreadLocalUtil.get();
        IPage<Admin> page  = new Page<>(adminQuery.getPage(), adminQuery.getLimit());
        return adminMapper.list(page, adminQuery);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        adminMapper.deleteAll(ids);
    }

    @Override
    public Admin selectByName(String name) {
        return adminMapper.selectByName(name);
    }

    @Override
    public Map<String, Object> selectAssignRole(Integer adminId) {
        List<Role> roleList = roleMapper.selectList(null);
        List<Long> assignedRoleIdList = adminRoleMapper.selectByAdminId(adminId);

        Map<String, Object> map = new HashMap<>();
        map.put("roleList", roleList);
        map.put("assignedRoleIdList", assignedRoleIdList);
        return map;
    }

    @Override
    public void assignRole(Long adminId, Long[] roleIds) {
        //在admin_role表里面先删除adminId已经分配的角色
        adminRoleMapper.deleteByAdminId(adminId);
        //给这个用户重新分配角色
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoleMapper.insert(adminRole);
        }
    }

    @Override
    public Map<String, Object> selectAdminPermissionInfoById(Integer id) {
        //根据id查找用户信息
        Admin admin = adminMapper.selectById(id);
        //根据admin的id查找菜单(路由)权限
        List<PermissionVO> routerVOList =  permissionServiceImpl.selectRouterTreeByAdminId(id);
        //根据admin的id查找按钮权限
        List<String> btnList = permissionServiceImpl.selectBtnListByAdminId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("admin", admin);
        map.put("routers", routerVOList);
        map.put("btns", btnList);
        return map;
    }


}
